from pymouse import PyMouse
import time
import pyautogui
import cv2
import numpy as np
from matplotlib import pyplot as plt 

m = PyMouse()

##  点选的操作
def doClick(index):
    pos_list = [
        [1658, 908],      # 点选Attack按钮
        [1322, 278],      # 点选关卡
        [1742, 999],      #  开始任务
        [910, 797],        #  结束点击操作1
        [1597, 1012],     #   结束点击操作2
        [1007, 497],        #   吃苹果
        [1228, 848]       # 吃苹果确定
        ]
    for i in range(1, 8):
        if (index == i):
            m.click(pos_list[i-1][0], pos_list[i-1][1])

##  释放 技能
def doSkill(character, skill, direction):
    x_list =[[107, 236, 385],            #第一个人物的三个技能x坐标
                 [574, 708, 838],
                 [1034, 1181, 1304]]
    direction_list = [[478, 620], [957, 620], [1396, 620]]          #选择人物的坐标
    time.sleep(1)
    m.click(x_list[character-1][skill-1], 870)
    if (direction != 0):
        time.sleep(1)
        m.click(direction_list[direction-1][0], direction_list[direction-1][1])
    time.sleep(3)

#施放御主技能
def doMySkill(index, direction):
    skill_list = [1321, 1466, 1581]
    direction_list = [[478, 620], [957, 620], [1396, 620]]          #选择人物的坐标
    time.sleep(1)
    m.click(1747, 480)
    time.sleep(1)
    m.click(skill_list[index-1], 480)
    if (direction != 0):
        time.sleep(1)
        m.click(direction_list[direction-1][0], direction_list[direction-1][1])
    time.sleep(3)

## 指令卡选择    TODO    宝具卡选择
def chooseCard(isbao, i):
    x_list = [193, 570, 945, 1317, 1699, 602, 938, 1286]
    y_list = [760, 323]
    m.click(x_list[i-1], y_list[isbao])
    time.sleep(0.5)

## 指令卡 截图 不保存
def cardScreenshotTouse(index):
    pos_list = [
        [90, 630, 220, 300],
        [460, 630, 220, 300],
        [830 , 630, 220, 300],
        [1200, 630, 220, 300],
        [1580, 630, 220, 300]
        ]
    img = pyautogui.screenshot(region=[pos_list[index-1][0], pos_list[index-1][1],
                                           pos_list[index-1][2], pos_list[index-1][3]]) # x,y,w,h
    img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
    return img

## 计算汉明距离 
def Hamming_distance(hash1,hash2): 
    num = 0
    for index in range(len(hash1)): 
        if (hash1[index] != hash2[index]): 
            num += 1
    return num

# 输入灰度图，返回hash 
def getHash(image): 
    avreage = np.mean(image) 
    hash = [] 
    for i in range(image.shape[0]): 
        for j in range(image.shape[1]): 
            if image[i,j] > avreage: 
                hash.append(1) 
            else: 
                hash.append(0) 
    return hash

##  得到两张图片的相似度
def classify_pHash(image1,image2): 
    image1 = cv2.resize(image1,(32,32)) 
    image2 = cv2.resize(image2,(32,32)) 
    gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY) 
    gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY) 
    # 将灰度图转为浮点型，再进行dct变换 
    dct1 = cv2.dct(np.float32(gray1)) 
    dct2 = cv2.dct(np.float32(gray2)) 
    # 取左上角的8*8，这些代表图片的最低频率 
    # 这个操作等价于c++中利用opencv实现的掩码操作 
    # 在python中进行掩码操作，可以直接这样取出图像矩阵的某一部分 
    dct1_roi = dct1[0:8,0:8] 
    dct2_roi = dct2[0:8,0:8] 
    hash1 = getHash(dct1_roi) 
    hash2 = getHash(dct2_roi) 
    return Hamming_distance(hash1,hash2)

#角色选择界面的角色选择  index决定选择的角色
def characChoose(index):
    chara_list = [
        'helpCharac\\Kongming.png',            #孔明   1
        'helpCharac\\Sikaha.png'                   #斯卡哈   2
        ]
    m.click(632, 209)   #选择术阶
    time.sleep(0.5)
    while(1):
        for j in range(0, 2):
            for i in range(277, 1055):
                img = pyautogui.screenshot(region=[81, i, 233, 38]) # x,y,w,h
                img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
                img2 = cv2.imread(chara_list[index-1])
                if (classify_pHash(img, img2) <= 2):
                    m.click(200, i)
                    return 1
            if (j == 0):
                m.click(1808, 737)        #向下滚动
        time.sleep(10)
        m.click(1245, 220)
        time.sleep(1)
        m.click(1230, 847)
        
##  做出各种目前界面的判断   是返回1  否返回0
def flagScreenshot(i):
    pos_list = [
        [171, 135, 339, 55],   #    游戏结束的第二个标志   1
        [1578, 949, 150, 53],  #  Attack 的标志                 2
        [112, 268, 347, 69],   #     游戏结束的第一个标志    3
        [666, 214, 135, 50],     #    苹果界面标志               4
        [1133, 236, 297, 52],   #  关卡界面标志                   5
        [1207, 183, 75, 71],     #   角色选择界面标志          6 
        [1647, 983, 172, 55],    #  开始关卡界面标志           7
        [951, 294, 275, 64]        #   游戏结束的第1.5个标志   8
        ]
    file_name = [
        'isEnd2.png',
        'canAttack.png',
        'isEnd.png',
        'apple.png',
        'level.png',
        'charac.png',
        'start.png',
        'isEnd1.png'
        ]
    img = pyautogui.screenshot(region=[pos_list[i-1][0], pos_list[i-1][1],
                                       pos_list[i-1][2], pos_list[i-1][3]]) # x,y,w,h
    img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
    img2 = cv2.imread(file_name[i-1])
    if (classify_pHash(img, img2) <= 2):
        return 1
    else:
        return 0

##    智能选卡函数    参数为三角色文件夹名称
def clearCC(charac1, charac2, charac3, index):
    card_list = [             #用以存取卡片标识
        [0, 0],        
        [0, 0],
        [0, 0],
        [0, 0],
        [0, 0]]
    charac = [charac1, charac2, charac3]
    img = [cardScreenshotTouse(1), cardScreenshotTouse(2),
           cardScreenshotTouse(3), cardScreenshotTouse(4),
           cardScreenshotTouse(5)]
    like_min = [9999, 9999, 9999, 9999, 9999]
    for i in range(0, 5):
        for j in range(0, 3):
            for h in range(1, 4):
                img_temp = cv2.imread(charac[j] + '\\' + str(h) + '.png')
                like = classify_pHash(img[i], img_temp)
                if (like <= like_min[i]):
                    like_min[i] = like
                    card_list[i] = [j+1, h]
    print(str(card_list))
    #   卡片智能选择
    pre_small = 0
    small = 0
    value = [0, 0, 0, 0, 0]
    for k in range(0, 5):
        value[k] = card_list[k][1] * 100 + card_list[k][0] * 101
    for z in range(0, index):
        for x in range(0, 5):
            if (value[x] < value[small]):
                small = x
        value[small] = 9999
        chooseCard(0, small+1)

time.sleep(4)
count = 0       #count代表回合数
pc = 0
while(pc != 3):
    if (flagScreenshot(5) == 1):            #选关界面的操作
        count = 0
        doClick(2)
    if (flagScreenshot(4) == 1):           #苹果界面的操作
        doClick(6)
        time.sleep(1)
        doClick(7)
    if (flagScreenshot(6) == 1):           #角色选择界面的操作
        characChoose(1)
    if (flagScreenshot(7) == 1):           #开始任务界面的操作
        doClick(3)
        #技能操作  TODO
    if (flagScreenshot(2) == 1):           #待选卡界面的操作
        count = count + 1
        print(str(count))
        if (count == 1):
            doSkill(2, 3, 0)
            doSkill(3, 2, 0)
            doSkill(3, 3, 0)
            doClick(1)
            time.sleep(2)
            chooseCard(1, 7)
            clearCC('JianDai', 'DaYingXiong', 'KongMing', 2)
        elif (count == 3):
            doSkill(2, 1, 0)
            doSkill(2, 2, 0)
            doClick(1)
            time.sleep(2)
            chooseCard(1, 7)
            clearCC('JianDai', 'ShuNiTuo', 'KongMing', 2)
        elif (count == 5):
            doSkill(2, 3, 0)
            doClick(1)
            time.sleep(2) 
            clearCC('JianDai', 'ShuNiTuo', 'KongMing', 3)
        elif (count == 6):
            doMySkill(1, 1)
            doMySkill(2, 1)
            doSkill(3, 1, 1)
            doSkill(1, 1, 0)
            doSkill(1, 2, 0)
            doSkill(1, 3, 0)
            doClick(1)
            time.sleep(2)
            chooseCard(1, 6)
            clearCC('JianDai', 'ShuNiTuo', 'KongMing', 2)
        else:
            doClick(1)
            time.sleep(2) 
            clearCC('JianDai', 'ShuNiTuo', 'KongMing', 3)
    if (flagScreenshot(3) == 1):    #关卡结束后第一个界面
        doClick(4)
        time.sleep(0.75)
    if (flagScreenshot(8) == 1):     #关卡结束后的界面
        doClick(4)
        time.sleep(0.75)
    if (flagScreenshot(1) == 1):      #关卡结束后的界面
        pc = pc + 1
        doClick(5)
    time.sleep(0.1)


